import { fetchAllButtons } from "@/api/button";
// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
const state = {
    /***
     * 按钮分为三大类
     * 1. 直接执行方法，样式类似为搜索重置按钮
     * 2.提交类型 直接执行表单数据处理或数据处理 样式为sbumit类型
     * 3.弹窗类型 弹出窗口触发 样式为新增 删除 编辑等类型
     * 用cssType 区分  当页面切换时，
     * 触发按钮匹配设置，vuex更新最新按钮状态及相关操作，自定义的按钮方法在业务组件进行设置
     */
    currentMenuButton: [{
            name: "新增",
            btnType: "add",
            cssType: "default",
            method: () => {
                console.log("新增");
            }
        },
        {
            name: "批量导入",
            btnType: "import",
            cssType: "default",
            method: () => {
                console.log("批量导入");
            }
        },
        {
            name: "导出",
            btnType: "export",
            cssType: "default",
            method: () => {
                console.log("导出");
            }
        },
        {
            name: "下载1",
            btnType: "download1",
            cssType: "default",
            method: () => {
                console.log("下载1");
            }
        },
        {
            name: "下载2",
            btnType: "download2",
            cssType: "default",
            method: () => {
                console.log("下载2");
            }
        },
        {
            name: "编辑",
            btnType: "edit",
            cssType: "default",
            method: () => {
                console.log("编辑");
            }
        },
        {
            name: "搜索",
            btnType: "search",
            cssType: "default",
            method: () => {
                console.log("搜索");
            }
        },
        {
            name: "重置",
            btnType: "reset",
            cssType: "default",
            method: () => {
                console.log("重置");
            }
        },
        {
            name: "查看",
            btnType: "view",
            cssType: "default",
            method: () => {
                console.log("下载");
            }
        },
        {
            name: "草稿",
            btnType: "remark",
            cssType: "default",
            method: () => {
                console.log("编辑");
            }
        },
        {
            name: "审批",
            btnType: "approve",
            cssType: "default",
            method: () => {
                console.log("审批");
            }
        },
        {
            name: "删除",
            btnType: "delete",
            cssType: "default",
            method: () => {
                console.log("删除");
            }
        },
        {
            name: "入库",
            btnType: "instock",
            cssType: "default",
            method: () => {
                console.log("入库");
            }
        },
        {
            name: "出库",
            btnType: "outstock",
            cssType: "default",
            method: () => {
                console.log("出库");
            }
        },
        {
            name: "重置密码",
            btnType: "resetPwd",
            cssType: "default",
            method: () => {
                console.log("出库");
            }
        }
    ],
    constBtns: [{
        name: '审批',
        btnType: 'approve',
        cssType: 'default',
        location: 'table',
        showInpage: 2 // 第一级页面显示该按钮
    }, {
        name: '分配',
        btnType: 'allocate',
        cssType: 'default',
        location: 'table',
        showInpage: 2 // 第一级页面显示该按钮
    }, {
        name: '挂起审核',
        btnType: 'awaitapprove',
        cssType: 'default',
        location: 'table',
        showInpage: 2 // 第一级页面显示该按钮
    }, {
        name: '验收',
        btnType: 'check',
        cssType: 'default',
        location: 'table',
        showInpage: 2 // 第一级页面显示该按钮
    }],
    currentButtonMethods: {}
};

const mutations = {
    SET_CURRENTBUTTON_RIGHT: (state, args) => {
        state.currentMenuButton = args;
    },
    SET_CURRENTBUTTON_METHODS: (state, args) => {
        console.log("传入的按钮事件");

        state.currentMenuButton.map(function(item, index) {
            console.log(item);
            // debugger
            if (item.btnType == "approve" || item.btnType == "check") {
                item.method = args["detail"];
            } else {
                item.method = args[item.btnType];
            }
        });
        //state.currentButtonMethods = args
    }
};

const actions = {
    // 设备台账
    setCurrentAllButtons({ commit, state }, args) {
        commit("SET_CURRENTBUTTON_RIGHT", args);
    },
    setCurrentAllButtonMethods({ commit, state }, args) {

        commit("SET_CURRENTBUTTON_METHODS", args);
    },
    async requestButtons({ commit, state }, args) {
        return await fetchAllButtons(args)
            .then(res => {
                if (res.data) {
                    const btnData = res.data.map(e => {
                        e.name = e.menuName;
                        const meta = JSON.parse(e.meta);
                        e.btnType = meta.btnType;
                        e.cssType = meta.cssType || "default";
                        e.showInpage = meta.showInpage || true;
                        e.location = meta.location;
                        e.method = () => {
                            console.log("初始方法");
                        };
                        return e;
                    });

                    const allBtn = btnData.concat(state.constBtns)
                    console.log("所有的结果数据", allBtn);
                    commit("SET_CURRENTBUTTON_RIGHT", allBtn);
                } else {}
            })
            .catch(err => {});
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};